package com.neusoft.databus.policy.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.databus.policy.dto.PolicyRefLinkageDTO;
import com.neusoft.databus.policy.model.PolicyRefLinkage;
import com.neusoft.databus.policy.repository.PolicyRefLinkageRepository;

/**
 * 策略资源关联类Converter
 *
 * @author naxiang
 */
@Component
public class PolicyRefLinkageConverter extends AbstractConverter<PolicyRefLinkage, PolicyRefLinkageDTO> {
    @Autowired
    private PolicyRefLinkageRepository policyRefLinkageRepository;

    @Override
    public PolicyRefLinkage toModel(PolicyRefLinkageDTO dto) {
        PolicyRefLinkage model;
        if (dto.isNew()) {
            model = new PolicyRefLinkage();
        } else {
            model = this.policyRefLinkageRepository.getOne(dto.getId());
        }
        model.setResourceCode(dto.getResourceCode());
        model.setControlUsageMetrics(dto.getControlUsageMetrics());
        model.setCancelUsageMetrics(dto.getCancelUsageMetrics());
        return model;
    }

    @Override
    public PolicyRefLinkageDTO toDTO(PolicyRefLinkage model, boolean forListView) {
        final PolicyRefLinkageDTO dto = new PolicyRefLinkageDTO();
        dto.setId(model.getId());
        dto.setResourceCode(model.getResourceCode());
        dto.setControlUsageMetrics(model.getControlUsageMetrics());
        dto.setCancelUsageMetrics(model.getCancelUsageMetrics());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        return dto;
    }

    public PolicyRefLinkage toModel(String resourceCode) {
        final PolicyRefLinkage model = new PolicyRefLinkage();
        model.setResourceCode(resourceCode);
        return model;
    }
}
